import 'package:cms/api/term.dart';
import 'package:common/export/bloc.dart';
import 'package:common/export/get_it.dart';
import 'package:common/lara_press_logs.dart';
import 'package:flutter/material.dart';
import 'package:user/state/user_action_history/user_action_history_cubit.dart';

import 'search_state.dart';

class SearchCubit extends Cubit<SearchState> {
  final TickerProvider? tickerProvider;

  SearchCubit({this.tickerProvider}) : super(SearchState.initial()) {
    init();
    laraPressGetIt<LaraPressUserActionHistoryCubit>().getSearchHistory();
  }
  void init() {
    TabController tabController = TabController(
      length: 2,
      vsync: tickerProvider!,
    );
    emit(state.copyWith(tabController: tabController));
    getHotCategory();
  }

  void getHotCategory() async {
    try {
      final response = await LaraPressTermApi().getTerms({'per_page': 10});
      emit(state.copyWith(hotCategory: response.data));
    } catch (e) {
      LaraPressLogs.getInstance().error(e.toString());
    }
  }

  Future<void> search({String? keyword}) async {
    try {
      if (keyword != null) {
        state.searchKeyword.text = keyword;
      }
      emit(state.copyWith(isSearch: true));
    } catch (e) {}
  }
}
